using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class HUDScreen : MonoBehaviour
{
	public Text characterName;
	public Image characterImage;
	public Text statHead;
	public Text statLArm;
	public Text statRArm;
	public Text statTorso;
	public Text statLLeg;
	public Text statRLeg;

	public RectTransform tabSeperator;
	public RectTransform btnInfo;
	public RectTransform btnEquipped;
	public RectTransform btnContents;
	public RectTransform btnActions;

	public RectTransform mainPanel;
	public RectTransform panelInit;
	
	public Text currentCharacterName;
	public Text nextCharacterName;

	private Character characterSelected;
	private List<HUDPanel> panels = new List<HUDPanel>();

	private static HUDScreen instance;
	public static HUDScreen Instance
	{
		get { return instance; }
	}

	private void Awake()
	{
		instance = this;
		Character.characterModified += UpdateCharacter;
		InitiativeManager.characterChanged += CharacterChanged;
		InitiativeManager.characterAdded += CharacterChanged;
	}

	private void Start()
	{
		SpawnPanel(PanelID.Panel_Info);
		SpawnPanel(PanelID.Panel_Equipped);
		SpawnPanel(PanelID.Panel_Contents);
		SpawnPanel(PanelID.Panel_Action);

		OnClick_Actions();
	}

	private void OnDestroy()
	{
		instance = null;
		Character.characterModified -= UpdateCharacter;
		InitiativeManager.characterChanged -= CharacterChanged;
		InitiativeManager.characterAdded -= CharacterChanged;
	}

	public void SetupCharacter(Character _character)
	{
		if (characterSelected != _character)
		{
			characterSelected = _character;
			characterName.text = _character.GetInfo().itemName;
			characterImage.sprite = _character.GetInfo().image;

			UpdateCharacter();
			/*if (_character != InitiativeManager.Instance.current)
			{
				OnClick_Info();
				btnActions.GetComponent<Button>().interactable = false;
			}
			else
			{
				OnClick_Actions();
				btnActions.GetComponent<Button>().interactable = true;
			}*/
		}
	}

	private void UpdateCharacter()
	{
		CharacterInfo info = characterSelected.GetInfo<CharacterInfo>();
		statHead.text = info.body.GetStat(BodyType.Head).GetPercentage() + "%";
		statLArm.text = info.body.GetStat(BodyType.L_Arm).GetPercentage() + "%";
		statRArm.text = info.body.GetStat(BodyType.R_Arm).GetPercentage() + "%";
		statTorso.text = info.body.GetStat(BodyType.Torso).GetPercentage() + "%";
		statLLeg.text = info.body.GetStat(BodyType.L_Leg).GetPercentage() + "%";
		statRLeg.text = info.body.GetStat(BodyType.R_Leg).GetPercentage() + "%";

		foreach (HUDPanel panel in panels)
		{
			panel.UpdatePanel(characterSelected);
		}
	}

	private void CharacterChanged()
	{
		Character character = InitiativeManager.Instance.current;
		currentCharacterName.text = character.GetInfo().itemName;
		currentCharacterName.color = character.GetInfo().mainColor;

		Character next = InitiativeManager.Instance.next;
		nextCharacterName.text = next.GetInfo().itemName;
		nextCharacterName.color = next.GetInfo().mainColor;
	}

	public HUDPanel GetPanel(PanelID _panelID)
	{
		foreach (HUDPanel panel in panels)
		{
			if (panel.selfID == _panelID)
			{
				return panel;
			}
		}

		return null;
	}
	
	public void OnClick_Info()
	{
		tabSeperator.SetAsLastSibling();
		btnInfo.SetAsLastSibling();
		EnablePanel(PanelID.Panel_Info);
	}
	
	public void OnClick_Equipped()
	{
		tabSeperator.SetAsLastSibling();
		btnEquipped.SetAsLastSibling();
		EnablePanel(PanelID.Panel_Equipped);
	}
	
	public void OnClick_Contents()
	{
		tabSeperator.SetAsLastSibling();
		btnContents.SetAsLastSibling();
		EnablePanel(PanelID.Panel_Contents);
	}
	
	public void OnClick_Actions()
	{
		tabSeperator.SetAsLastSibling();
		btnActions.SetAsLastSibling();
		EnablePanel(PanelID.Panel_Action);
	}

	public void OnClick_CurrentCharacter()
	{
		SetupCharacter(InitiativeManager.Instance.current);
	}

	public void OnClick_NextCharacter()
	{
		SetupCharacter(InitiativeManager.Instance.next);
	}
	
	public void OnClick_Menu()
	{
		
	}
	
	public void OnClick_Settings()
	{
		
	}
	
	public void OnClick_SelectCharacter()
	{
		
	}
	
	private void EnablePanel(PanelID _id)
	{
		foreach (HUDPanel panel in panels)
		{
			panel.gameObject.SetActive(panel.selfID == _id);
		}
	}
	
	private void SpawnPanel(PanelID _id)
	{
		GameObject obj = PoolManager.Instance.GetFromPool(_id.ToString());
		ScreensManager.Instance.SetParent(obj, mainPanel);
		
		HUDPanel panel = obj.GetComponent<HUDPanel>();
		panels.Add(panel);
	}
}
